﻿package mortal.common.swfPlayer
{
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.common.swfPlayer.frames.*;

    public class BitmapPlayer extends Bitmap implements IDispose
    {
        private var _source:FrameArray;
        private var _totalFrames:int;
        private var _currentFrame:int;
        protected var _timeRate:int = 0;
        protected var _defaultTimeRate:int = 6;
        protected var _bitmapFrame:BitmapFrame;
        private var _frameScript:Dictionary;
        protected var _isUpdate:Boolean = false;
        public var framesPlayerCompleteHandler:Function;
        private var _sceneX:Number = 0;
        private var _sceneY:Number = 0;
        protected var _isTurn:Boolean = false;
        public var useByModelShowTool:Boolean = false;

        public function BitmapPlayer()
        {
            this._frameScript = new Dictionary();
            return;
        }// end function

        public function get timeRate() : int
        {
            if (this._timeRate > 0)
            {
                return this._timeRate;
            }
            if (this._source)
            {
                return this._source.delay;
            }
            return this._defaultTimeRate;
        }// end function

        public function addFrameScript(param1:int, param2:Function) : void
        {
            if (param1 >= 0)
            {
                this._frameScript[param1] = param2;
            }
            return;
        }// end function

        public function removeFrameScripte(param1:int) : void
        {
            this._frameScript[param1] = null;
            delete this._frameScript[param1];
            return;
        }// end function

        public function get currentFrame() : int
        {
            return this._currentFrame;
        }// end function

        public function set currentFrame(param1:int) : void
        {
            this._currentFrame = param1;
            return;
        }// end function

        public function get totalFrames() : int
        {
            return this._totalFrames;
        }// end function

        public function set source(param1:FrameArray) : void
        {
            this._source = param1;
            if (this._source)
            {
                this._totalFrames = this._source.length;
                this._isTurn = this._source.isTurn;
            }
            else
            {
                this._isTurn = false;
            }
            return;
        }// end function

        public function get source() : FrameArray
        {
            return this._source;
        }// end function

        public function nextFrame() : void
        {
            if (this._totalFrames > 0)
            {
                (this._currentFrame + 1);
                if (this._currentFrame > (this._totalFrames - 1))
                {
                    this._currentFrame = 0;
                }
            }
            return;
        }// end function

        public function preFrame() : void
        {
            if (this._totalFrames > 0)
            {
                (this._currentFrame - 1);
                if (this._currentFrame < 0)
                {
                    this._currentFrame = this._totalFrames - 1;
                }
            }
            return;
        }// end function

        public function clear() : void
        {
            bitmapData = null;
            this._currentFrame = 0;
            this._totalFrames = 0;
            this._source = null;
            this._sceneX = 0;
            this._sceneY = 0;
            this._timeRate = 0;
            return;
        }// end function

        protected function framesPlayerComplete() : void
        {
            if (this.framesPlayerCompleteHandler is Function)
            {
                this.framesPlayerCompleteHandler.call(null, this);
            }
            return;
        }// end function

        public function updateCurrentFrame(param1:int) : void
        {
            if (this._source)
            {
                this._bitmapFrame = this._source[param1];
                if (this._bitmapFrame)
                {
                    if (this._bitmapFrame.bitmapData)
                    {
                        if (bitmapData != this._bitmapFrame.bitmapData)
                        {
                            bitmapData = this._bitmapFrame.bitmapData;
                        }
                        if (this._isTurn)
                        {
                            this.scaleX = -1;
                            this.setBitmapXY(-this._bitmapFrame.x, this._bitmapFrame.y);
                        }
                        else
                        {
                            this.scaleX = 1;
                            this.setBitmapXY(this._bitmapFrame.x, this._bitmapFrame.y);
                        }
                    }
                    else if (bitmapData != null)
                    {
                        bitmapData = null;
                    }
                }
                else
                {
                    return;
                }
                if ((this._currentFrame + 1) == this._totalFrames)
                {
                    this.framesPlayerComplete();
                }
                if (this._frameScript[this._currentFrame] is Function)
                {
                    this._frameScript[this._currentFrame].apply(null);
                }
            }
            else
            {
                this.bitmapData = null;
            }
            return;
        }// end function

        public function gotoAndStop(param1:int = 0) : void
        {
            if (this._source)
            {
                this._bitmapFrame = this._source[param1];
                if (this._bitmapFrame)
                {
                    if (this._bitmapFrame.bitmapData)
                    {
                        if (bitmapData != this._bitmapFrame.bitmapData)
                        {
                            bitmapData = this._bitmapFrame.bitmapData;
                        }
                        if (this._isTurn)
                        {
                            this.scaleX = -1;
                            this.setBitmapXY(-this._bitmapFrame.x, this._bitmapFrame.y);
                        }
                        else
                        {
                            this.scaleX = 1;
                            this.setBitmapXY(this._bitmapFrame.x, this._bitmapFrame.y);
                        }
                    }
                    else if (bitmapData != null)
                    {
                        bitmapData = null;
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                this.bitmapData = null;
            }
            return;
        }// end function

        protected function setBitmapXY(param1:Number, param2:Number) : void
        {
            var _loc_3:Point = null;
            var _loc_4:Point = null;
            if (this.useByModelShowTool)
            {
                this.x = this._sceneX;
                this.y = this._sceneY;
            }
            else
            {
                this.x = param1 + this._sceneX;
                this.y = param2 + this._sceneY;
            }
            if (rotation != 0 && this._bitmapFrame)
            {
                _loc_3 = this.getCorrectPoint(-1 * this._bitmapFrame.x, -1 * this._bitmapFrame.y);
                _loc_4 = this.getCorrectPoint(0, 0);
                this.x = this.x + (_loc_3.x - _loc_4.x);
                this.y = this.y + (_loc_3.y - _loc_4.y);
            }
            return;
        }// end function

        private function getCorrectPoint(param1:Number, param2:Number) : Point
        {
            var _loc_3:* = new Matrix();
            var _loc_4:* = new Point(param1, param2);
            _loc_3.tx = _loc_3.tx - _loc_4.x;
            _loc_3.ty = _loc_3.ty - _loc_4.y;
            if (rotation % 360 != 0)
            {
                _loc_3.rotate(rotation * Math.PI / 180);
            }
            _loc_3.tx = _loc_3.tx + _loc_4.x;
            _loc_3.ty = _loc_3.ty + _loc_4.y;
            return new Point(_loc_3.tx, _loc_3.ty);
        }// end function

        public function get sceneX() : Number
        {
            return this._sceneX;
        }// end function

        public function set sceneX(param1:Number) : void
        {
            this._sceneX = param1;
            return;
        }// end function

        public function get sceneY() : Number
        {
            return this._sceneY;
        }// end function

        public function set sceneY(param1:Number) : void
        {
            this._sceneY = param1;
            return;
        }// end function

        public function move(param1:Number, param2:Number) : void
        {
            this._sceneX = param1;
            this._sceneY = param2;
            return;
        }// end function

        public function play() : void
        {
            return;
        }// end function

        public function stop() : void
        {
            return;
        }// end function

        public function getMaxY() : Number
        {
            var _loc_1:* = this.getBounds(this);
            return Math.abs(_loc_1.y);
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            this._frameScript = new Dictionary();
            this.framesPlayerCompleteHandler = null;
            return;
        }// end function

    }
}
